package Bio::Rambam::Proteomics::Mgf;

use Moose;
use namespace::autoclean;
use Data::Dumper;
use Bio::Rambam::Proteomics::Ms2;
extends 'Bio::Rambam::Proteomics::Formats::Peaklist';
with 'Bio::Rambam::Proteomics::Tools::FileGeneration';



sub add_spectrum_mgf{
	my ($self, $spectrum_object) = @_;
	return unless $spectrum_object;
	return unless $spectrum_object->id;
	return unless $spectrum_object->precursor_mz;
	return unless $spectrum_object->array_int_translated;
	return unless $spectrum_object->array_mz_translated;
	
	open MGF,">>",$self->file;
	my $mgf_TITLE=$self->filename;
	$mgf_TITLE=~s/\.mgf$//;
	print MGF "\nBEGIN IONS\n";
	# The TITLE: I'm using the mzml->run_id->spectrumList->spectrum->@id
	print MGF  "TITLE=file|".$mgf_TITLE."|index|".$spectrum_object->index."|\n";
	#print MGF  "TITLE=".$self->filename."\.".$spectrum_object->index."\.".$spectrum_object->index."\n";
	print MGF "PEPMASS=".sprintf("%.4f",$spectrum_object->precursor_mz) if defined $spectrum_object->precursor_mz;
	if(defined $spectrum_object->precursor_int){
		print MGF " ".sprintf("%.0f",$spectrum_object->precursor_int());
	}
	print MGF "\n" if defined $spectrum_object->precursor_mz;
	print MGF "CHARGE=".$spectrum_object->precursor_z."\n" if defined $spectrum_object->precursor_z;
	for(my $i=0;$i<=$#{$spectrum_object->array_mz_translated};$i++){
		print MGF sprintf("%.4f",${$spectrum_object->array_mz_translated}[$i]);
		print MGF " ";
		print MGF sprintf("%.2f",${$spectrum_object->array_int_translated}[$i]);
		print MGF "\n";
	}
	print MGF "\nEND IONS\n";
	close MGF;
	
}


sub convert_to_ms2{
	my ($self,%opt)=@_;
	my ($output_folder,$output_filename)=_create_outputfile($self->file,'mgf|MGF|Mgf','ms2',%opt);
	print "Generating $output_filename in $output_folder...";
	my $ms2_text;
	my $spec_index=0;
	{
		local $/="END IONS\n";
		open my $mgf,"<",$self->file;		
		while(<$mgf>){
			my @lines=split /\n/,$_;
			$spec_index++;
			my (@spec,$title,$pepmass,$file,$scan);
			foreach my $line(@lines){
				#print $line,"\n";
				$title=$1 if $line=~/^TITLE\=(.+)/;
				$pepmass=$1 if $line=~/^PEPMASS\=(.+)/;
				push @spec,$line if $line=~/^\d/;
			}
			#print "$title\n";next;
			#next unless $file;
			$file=(split /\|/,$title)[1];
			$scan=(split /\|/,$title)[3];
			$pepmass=(split / /,$pepmass)[0];
			
			#print $file,"n";
			die "No FILE in one spectrum\n" unless $file;		### Mira esto: he puesto defined ante el caso de scan=0 (RGR_2013_359_Fr13_130222_03.mgf)
			die "No SCAN in one spectrum\n" unless defined $scan;
			die "No PEPMASS in one spectrum\n" unless defined $pepmass;
			#my $title=TITLE=file|kk|index|2|
			#PEPMASS=644.3593 7921571
			$ms2_text.="H	Source file	$file\n";
			$ms2_text.="S	$spec_index	$spec_index	$pepmass\n";# Use as start and end scan the order of the spec in the original mgf
			#$ms2_text.="S	$scan	$scan	$pepmass\n";
			foreach(@spec){
				$ms2_text.=$_."\n";
			}
			$ms2_text.="\n\n";
		}
	}
	
	open my $ms2,">",$output_folder.$output_filename;
	print $ms2 " ";	##### Dirty fix to the deleting of first character problem
	print $ms2 $ms2_text;
	close $ms2;
	my $ms2_file=Bio::Rambam::Proteomics::Ms2->new(file=>$output_folder.$output_filename);
	print "  OK\n";
	return $ms2_file;
}




__PACKAGE__->meta->make_immutable;